package controllers

import (
	"svntool/application/models/tokens"
	"svntool/db/tables"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

func BuildJson(status int, info string, data interface{}) map[string]interface{} {
	return map[string]interface{}{"status": status, "info": info, "data": data}
}

type BaseController struct {
	beego.Controller
}

func (b *BaseController) Prepare() {
	uid := tokens.Token_parse_model.GetUid(b.Ctx)

	o, user_roles, user_roles_es := orm.NewOrm(), new(tables.UserRoles), []tables.UserRoles{}

	b.Data["uid"] = uid

	if uid > 0 {
		//读取用户信息
		user := tables.Users{Id: uid}
		err := o.Read(&user)

		if err == nil {

			b.Data["Name"], b.Data["username"] = user.Name, user.Username

			if num, _ := o.QueryTable(user_roles).Filter("uid", uid).OrderBy("roleid__sort").All(&user_roles_es, "Roleid"); num == 0 {
				b.Data["Roles"] = nil
			} else {
				b.Data["Roles"] = user_roles_es
			}
		}
	}

}
